package com.vlefen.springcloud.grpc.grpc;

import io.grpc.Attributes;
import io.grpc.NameResolver;
import org.springframework.cloud.client.discovery.DiscoveryClient;

import javax.annotation.Nullable;
import java.net.URI;

public class NameResolverFactory extends NameResolver.Factory {

    private DiscoveryClient discoveryClient;

    private String serviceName;

    public NameResolverFactory(String serviceName, DiscoveryClient discoveryClient) {
        this.discoveryClient = discoveryClient;
        this.serviceName = serviceName;
    }

    @Nullable
    @Override
    public NameResolver newNameResolver(URI uri, Attributes attributes) {
        return new DiscoveryNameResolver(serviceName, discoveryClient);
    }

    @Override
    public String getDefaultScheme() {
        return "http";
    }
}
